using System;
using System.Linq;
using BruteForce.Characters;
using BruteForce.GameObjects;
using BruteForce.NewManagers;
using BruteForce.Screens.PlayingScreen.Map;
using BruteForce.UserInterface.Abstracts;
using BruteForce.UserInterface.UIControls;
using BruteForce.Utilities;
using Bruteforce.UserInterface.UIControls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.Screens.PlayingScreen.Panels
{
    public class PlayerStatusPanel : AbstractPanel
    {
        private readonly bool _showCreateButton;

        public PlayerStatusPanel(bool toShowCreateButton)
            : base("PlayerStatusPanel")
        {
            _showCreateButton = toShowCreateButton;

            //var MenuItemName = "Player\n"+"number";
            //var diffBetweenLines = Draws.CalculateWordSizeNormalFont(MenuItemName);
            //var screenBounds = CalculteCenteredRectangle(panelRectangle, (int)diffBetweenLines.X, (int)Draws.NormalFontSize.Y * 2);
            //Label abstractMenuItem = new Label(MenuItemName, new Rectangle(screenBounds.X, screenBounds.Y, (int)diffBetweenLines.X, (int)diffBetweenLines.Y));
            //AddUIControl(abstractMenuItem);

            /*
            var screenBounds = panelRectangle;
            var Name = "Shooter";
            var img = GameLoop.TileShooter;
            var imgSuroundingRectangle = img.Bounds;
            Image abstractMenuItem = new Image(img, Name, new Rectangle(screenBounds.X, screenBounds.Y,imgSuroundingRectangle.Width,imgSuroundingRectangle.Height));
            AddUIControl(abstractMenuItem);
             * */
        }


        public override void Update(GameTime gameTime)
        {
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Update();
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.GreyStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Draw(gameTime);
            }
        }


        public override void AfterLayout()
        {
            const int diffBetweenUIcontents = 15;
            Vector2 diffBetweenLines = Draws.NormalFontSize;


            ExitCharacterButton(diffBetweenUIcontents);
            CreateNewCharacterButton(diffBetweenUIcontents);
            CreateSwapTurnsButton(diffBetweenUIcontents);
            CreateLogoImage(diffBetweenUIcontents);

            var diffX = CreateImageForPlayer(diffBetweenUIcontents);
            

            CreateResorcesStatusLabels(diffBetweenUIcontents, diffBetweenLines, diffX);


            //todo change the number of difference in x
//            CreateResorcesPerTurnLabels(diffBetweenUIcontents, diffBetweenLines, Bounds.Width - 50);



//            //create "      ResurcesLeftToUseInTurn :" InteractiveLabel
//            string labelText = "      ResurcesLeftToUseInTurn: ";
//            Rectangle overallRectangle = new Rectangle(diffX + diffBetweenUIcontents +400, diffBetweenUIcontents + 2 * (int)diffBetweenLines.Y,
//                                            diffBetweenUIcontents + 2 * (int)diffBetweenLines.X,
//                                            (int)diffBetweenLines.Y);
//            CreateInteractiveLabel(GetPlayerResourcesAllowedLeftToUse,labelText, overallRectangle);



            //CreateSwitchTurnsButton(diffX + diffBetweenUIcontents + 400, diffBetweenUIcontents + 4 * (int)diffBetweenLines.Y);



        }

        private int CreateImageForPlayer(int diffBetweenUIcontents)
        {
            Texture2D img = UIControlManager.Instance.HumanPlayerTexture;
            Rectangle overAllRectangle = new Rectangle(diffBetweenUIcontents, diffBetweenUIcontents, img.Width,
                                                       img.Height);


            CreateInteractiveImage(GetPlayerImage, img,overAllRectangle);

            int diffX = img.Width + diffBetweenUIcontents;

            return diffX;
        }

        private void CreateNewCharacterButton(int diffBetweenUIcontents)
        {
            if (_showCreateButton)
            {
                Texture2D image = UIControlManager.Instance.CreateNewUnitButtonTexture;
                Texture2D image1 = UIControlManager.Instance.CreateNewUnitPressedButtonTexture;
                var abstractMenuItem = new DynamicButton(image,image1, "Create Button",
                                                  new Rectangle(Bounds.Width - image.Width*7 + diffBetweenUIcontents,
                                                                (Bounds.Height - image.Height)/2, image.Width,
                                                                image.Height),IsThereAnyTowersCaptured);
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                abstractMenuItem.OnClick += (() => ScreenManager.Instance.OpenNewCharacterScreen());
                AddUIControl(abstractMenuItem);
            }
        }

        private void CreateSwapTurnsButton(int diffBetweenUIcontents)
        {
            if (_showCreateButton)
            {
                Texture2D image = UIControlManager.Instance.EndTurnButtonTexture;
                Texture2D image1 = UIControlManager.Instance.EndTurnButtonPressedTexture;
                var abstractMenuItem = new DynamicButton(image, image1, "Create Button",
                                                  new Rectangle(Bounds.Width - image.Width * 8 - diffBetweenUIcontents,
                                                                (Bounds.Height - image.Height) / 2, image.Width,
                                                                image.Height), isGameOver);
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                abstractMenuItem.OnClick += (() => PlayerManager.Instance.SwitchTurns());
                AddUIControl(abstractMenuItem);
            }
        }

        private static bool isGameOver()
        {
            return !GamePanel.Current.gameOver;
        }

        private void ExitCharacterButton(int diffBetweenUIcontents)
        {
            if (_showCreateButton)
            {
                Texture2D image = UIControlManager.Instance.ExitButton;
                var abstractMenuItem = new Button(image, "Create Button",
                                                  new Rectangle(Bounds.Width - image.Width - diffBetweenUIcontents,
                                                                (Bounds.Height - image.Height) / 2, image.Width,
                                                                image.Height));
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                abstractMenuItem.OnClick += (() => ScreenManager.Instance.PopScreen());
                AddUIControl(abstractMenuItem);
            }
        }


        private void CreateLogoImage(int diffBetweenUIcontents)
        {

                Texture2D image = UIControlManager.Instance.BlackLogoTexture;
                var abstractMenuItem = new Image(image, "logo",
                                                  new Rectangle(Bounds.Width - image.Width*2 - diffBetweenUIcontents-30,
                                                                (Bounds.Height - image.Height) / 2, image.Width,
                                                                image.Height));
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                AddUIControl(abstractMenuItem);
        }



        private bool IsThereAnyTowersCaptured()
        {
            return  isGameOver() && PlayerManager.Instance.CurrentPlayer.Property.Towers.Any(mapCell => mapCell.CharacterInCell is Defender);
        }


        private void CreateSwitchTurnsButton(int xLocation,int yLocation)
        {
            if (_showCreateButton)
            {
                Texture2D image = UIControlManager.Instance.EndTurnButtonTexture;
                var abstractMenuItem = new Button(image, "Create Switch Turns Button", new Rectangle(xLocation,  yLocation, image.Width, image.Height));
                abstractMenuItem.SetOffset(new Vector2(Bounds.X, Bounds.Y));
                abstractMenuItem.OnClick += (() => PlayerManager.Instance.SwitchTurns());
                AddUIControl(abstractMenuItem);
            }




        }

        //********************************** "Resources Status" column ******************************************//
        private void CreateResorcesStatusLabels(int diffBetweenUIcontents, Vector2 diffBetweenLines, int diffX)
        {
//            //create "Human Status :" label
//            string labelText = "   Human Status";
//            
//            int textLabelHight = (int)Draws.NormalFontSize.Y;
//
//            var label = new Label("Human Status label", labelText,
//                                  new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents,
//                                                (int) diffBetweenLines.X,
//                                                textLabelHight));
//            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
//            AddUIControl(label);


            int textLabelHight = (int)Draws.NormalFontSize.Y;

            //create "      Human/Monster :" InteractiveLabel
            string labelText = "       ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            Rectangle overallRectangle =  new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents,
                                                (int) diffBetweenLines.X,
                                                textLabelHight);
            CreateInteractiveLabel(GetPlayerType, labelText, overallRectangle);




            //create "      Life :" InteractiveLabel
            labelText = "      Food : ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);
            overallRectangle = new Rectangle(diffX + diffBetweenUIcontents,
                                                       diffBetweenUIcontents + 2*textLabelHight,
                                                       (int) diffBetweenLines.X, textLabelHight);
            CreateInteractiveLabel(GetPlayerLife,labelText, overallRectangle);


            //create "      Light :" InteractiveLabel
            labelText = "      Sun Powder: ";
            overallRectangle = new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 3*textLabelHight,
                                             (int) diffBetweenLines.X,
                                             textLabelHight);

            CreateInteractiveLabel(GetPlayerLight,labelText, overallRectangle);

            //create "      Speed :" InteractiveLabel
            labelText = "      Speed Potion: ";
//            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);


            overallRectangle = new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 4*textLabelHight,
                                             (int) diffBetweenLines.X,
                                             textLabelHight);

            CreateInteractiveLabel(GetPlayerSpeed,labelText, overallRectangle);

            //create "      Power :" InteractiveLabel
            labelText = "      Iron: ";
            overallRectangle = new Rectangle(diffX + diffBetweenUIcontents, diffBetweenUIcontents + 5*textLabelHight,
                                             (int) diffBetweenLines.X,
                                             textLabelHight);
            CreateInteractiveLabel(GetPlayerPower,labelText, overallRectangle);
        }


        //******************************************* "Resources Per Turn" column **********************************//
        private void CreateResorcesPerTurnLabels    (int diffBetweenUIcontents, Vector2 diffBetweenLines, int diffX)
        {
            string labelText;
            int textLabelHight;

            

            //create "Resources Per Turn :" InteractiveLabel
            labelText = "   Resources Per Turn";

            int xLocation = diffX - 2* (int)Draws.CalculateWordSizeNormalFont(labelText).X ;
            textLabelHight = (int) diffBetweenLines.Y;
            var label = new Label("Resources Per Turn label", labelText,
                                  new Rectangle(xLocation, diffBetweenUIcontents,
                                                (int) diffBetweenLines.X,
                                                textLabelHight));
            label.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);



            //create "      Life :" interactiveLabel
            labelText = "      Food : ";
            diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);


            Rectangle overallRectangle = new Rectangle(xLocation,
                                                       diffBetweenUIcontents + 2*textLabelHight,
                                                       (int) diffBetweenLines.X,
                                                       textLabelHight);

            CreateInteractiveLabel(GetPlayerLifePerTurn,labelText, overallRectangle);

            //create "      Light :" interactiveLabel
            labelText = "      Sun Powder: ";
            overallRectangle = new Rectangle(xLocation,
                                             diffBetweenUIcontents + 3*textLabelHight,
                                             (int) diffBetweenLines.X,
                                             textLabelHight);


            CreateInteractiveLabel(GetPlayerLightPerTurn,labelText, overallRectangle);

            //create "      Speed :" interactiveLabel
            labelText = "      Speed Potion: ";

            overallRectangle = new Rectangle(xLocation,
                          diffBetweenUIcontents + 4*textLabelHight,
                          (int) diffBetweenLines.X,
                          textLabelHight);

            CreateInteractiveLabel(GetPlayerSpeedPerTurn , labelText, overallRectangle);

            //create "      Power :" interactiveLabel
            labelText = "      Iron: ";

            overallRectangle = new Rectangle(xLocation,
                                             diffBetweenUIcontents + 5*textLabelHight,
                                             (int) diffBetweenLines.X,
                                             textLabelHight);



            CreateInteractiveLabel(GetPlayerPowerPerTurn,labelText, overallRectangle);
        }


        private void CreateInteractiveImage(Func<Texture2D> method, Texture2D img, Rectangle overallRectangle)
        {
            var imageContent = new DynamicImage(img, "DynamicImage",
                                                overallRectangle, method);
            imageContent.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(imageContent);
        }

        private void CreateInteractiveLabel(Func<string> method,string labelText, Rectangle overallRectangle)
        {
            var InteractiveLabel = new InteractiveLabel("InteractiveLabel", labelText, overallRectangle, method);
            InteractiveLabel.SetOffset(new Vector2(Bounds.X, Bounds.Y));
            InteractiveLabel.SetLabelTextColor(Color.Black);
            AddUIControl(InteractiveLabel);
        }

        #region DelgatedFunctions


        public string GetPlayerLife()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.CurrentResources.Life.ToString() 
                + "(" + GetPlayerLifePerTurn() + ")";
        }


        public string GetPlayerLight()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.CurrentResources.Light.ToString()
                + "(" + GetPlayerLightPerTurn() + ")";
        }


        public string GetPlayerSpeed()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.CurrentResources.Speed.ToString()
                + "(" + GetPlayerSpeedPerTurn() + ")";
        }


        public string GetPlayerPower()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.CurrentResources.Power.ToString()
                + "(" + GetPlayerPowerPerTurn() + ")";
        }


        public string GetPlayerLifePerTurn()
        {
            return (PlayerManager.Instance.CurrentPlayer.Property.ResourcesPerTurn.Life-
                PlayerManager.Instance.CurrentPlayer.Property.GetSumLifesOfAllCharacters()).ToString();
        }


        public string GetPlayerLightPerTurn()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.ResourcesPerTurn.Light.ToString();
        }


        public string GetPlayerSpeedPerTurn()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.ResourcesPerTurn.Speed.ToString();
        }


        public string GetPlayerPowerPerTurn()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.ResourcesPerTurn.Power.ToString();
        }

        public string GetPlayerResourcesAllowedLeftToUse()
        {
            return PlayerManager.Instance.ResourcesAllowedLeftToUse.ToString();
        }

        public string GetPlayerType()
        {
            return PlayerManager.Instance.CurrentPlayer.Property.PlayerType.ToString();
        }


        //todo - change the the images
        private static Texture2D GetPlayerImage()
        {
            //create Image for the Player
            Texture2D img = null;
            switch (PlayerManager.Instance.CurrentPlayer.Property.PlayerType)
            {
                case PlayerType.Human:
                    img = UIControlManager.Instance.HumanPlayerTexture;
                    break;
                case PlayerType.Monster:
                    img = UIControlManager.Instance.MonsterPlayerTexture;
                    break;
            }
            return img;
        }

        #endregion
    }
}